package com.dunshan.deeptest.controller;

import com.alibaba.fastjson.JSON;
import com.dunshan.deeptest.pojo.Result;
import com.dunshan.deeptest.pojo.ScriptParam;
import com.dunshan.deeptest.service.ScriptService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.util.HashMap;

/**
 * @author .dunshan
 * @program: dunshan-pef
 * @description: 脚本管理
 * @date 2021-12-30 13:54:43
 */
@RestController
public class ScriptController {

    @Autowired
    ScriptService scriptService;

    /**
     * url解析
     *
     * @return
     */
    @PostMapping("/parse")
    public Result printParst(@PathParam("param") String param) {
        if (param.startsWith("http//") || param.startsWith("https://")) {
            return scriptService.printUrlInfo(param);
        }
        return Result.buildFailed();
    }

    @PostMapping("/test/run")
    public Result testRun(ScriptParam scriptParam) {
        return scriptService.testRun(scriptParam);
    }

    /**
     * 保存脚本
     *
     * @param scriptParam
     * @return
     */
    @PostMapping(value = "/save", produces = "application/json;charset=utf-8")
    public Result save(@RequestBody ScriptParam scriptParam) {

        return scriptService.save(scriptParam);
//        return Result.buildSuccess(JSON.toJSON(scriptParam));
    }


    /**
     * 更新
     *
     * @param scriptParam
     * @return
     */
    @PutMapping("/update")
    public Result update(ScriptParam scriptParam) {
        return scriptService.update(scriptParam);
    }


    /**
     * 删除
     *
     * @param id
     * @return
     */
    @DeleteMapping("/delete")
    public Result delete(Integer id) {
        return scriptService.delete(id);
    }

}
